package com.iwish.server.storage;



import java.net.UnknownHostException;

import org.apache.commons.pool.BasePoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;

import com.iwish.server.storage.util.ConfigHelper;
import com.mongodb.Mongo;

import redis.clients.jedis.BinaryJedis;
import redis.clients.jedis.Jedis;
import redis.clients.util.Pool;

// Referenced classes of package redis.clients.jedis:
//            BinaryJedis, Jedis

public class MongoPool extends Pool
{
    private static class MongoFactory extends BasePoolableObjectFactory
    {

        public Object makeObject()
            throws Exception
        {
        	  Mongo mg = null;
				
					mg = new Mongo(ConfigHelper.MONGO_HOST,ConfigHelper.MONGO_PORT);
				
				return mg;
        }

        public void destroyObject(Object obj)
            throws Exception
        {
            if(obj instanceof Mongo)
            {
            	Mongo mongo = (Mongo)obj;
            	mongo.close();
            }
        }

        public boolean validateObject(Object obj)
        {	
            if(obj instanceof Mongo)
            {
            	Mongo mongo = (Mongo)obj;
                try
                {
                	return mongo.getDatabaseNames().size()>=0;
                }
                catch(Exception e)
                {
                    return false;
                }
            } else
            {
                return false;
            }
        }

        private final String host;
        private final int port;
        private final int timeout;
        private final String password;
        private final int database;

        public MongoFactory(String host, int port, int timeout, String password, int database)
        {
            this.host = host;
            this.port = port;
            this.timeout = timeout;
            this.password = password;
            this.database = database;
        }
    }


    public MongoPool(org.apache.commons.pool.impl.GenericObjectPool.Config poolConfig, String host)
    {
        this(poolConfig, host, 27017, 2000, null, 0);
    }

    public MongoPool(String host, int port)
    {
        this(new org.apache.commons.pool.impl.GenericObjectPool.Config(), host, port, 2000, null, 0);
    }
    private  String alias;
    public String getAlisa(){
    	return this.alias;
    }
    public void setAlisa(String a){
    	this.alias=a;
    }
    public MongoPool(String host)
    {
        this(host, 27017);
    }

    public MongoPool(org.apache.commons.pool.impl.GenericObjectPool.Config poolConfig, String host, int port, int timeout, String password)
    {
        this(poolConfig, host, port, timeout, password, 0);
    }

    public MongoPool(org.apache.commons.pool.impl.GenericObjectPool.Config poolConfig, String host, int port)
    {
        this(poolConfig, host, port, 2000, null, 0);
    }

    public MongoPool(org.apache.commons.pool.impl.GenericObjectPool.Config poolConfig, String host, int port, int timeout)
    {
        this(poolConfig, host, port, timeout, null, 0);
    }

    public MongoPool(org.apache.commons.pool.impl.GenericObjectPool.Config poolConfig, String host, int port, int timeout, String password, int database)
    {
        super(poolConfig, new MongoFactory(host, port, timeout, password, database));
    }

  
}


/*
	DECOMPILATION REPORT

	Decompiled from: D:\sysfile\javalib\jedis-2.1.0.jar
	Total time: 709 ms
	Jad reported messages/errors:
	Exit status: 0
	Caught exceptions:
*/